package cn.risebd.wechat.pay.support;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.google.common.io.ByteStreams;

import cn.risebd.common.date.Dates;
import cn.risebd.wepay.core.Wepay;
import cn.risebd.wepay.core.WepayBuilder;
import cn.risebd.wepay.model.pay.JsPayRequest;
import cn.risebd.wepay.model.pay.JsPayResponse;
import cn.risebd.wepay.model.pay.QrPayRequest;
import cn.risebd.wepay.model.refund.RefundApplyRequest;
import cn.risebd.wepay.model.refund.RefundApplyResponse;

/**
 * @category 微信支付支持配置
 * @author Admin
 *
 */
@Service
public class WepaySupport {

	@Value("${appId}")
	private String appId;

	@Value("${appKey}")
	private String appKey;

	@Value("${mchId}")
	private String mchId;

	@Value("${certPasswd}")
	private String certPasswd;

	@Value("${certPath}")
	private String certPath;

	@Value("${payNotifyUrl}")
	private String payNotifyUrl;

	private Wepay wepay;

	/**
	 * @category 初始化微信支付
	 */
	@PostConstruct
	public void initWepay() {
		InputStream in = null;
		try {
			in = new FileInputStream(new File(certPath));// this.getClass().getClassLoader().getResourceAsStream("cert.p12")
			// 加载证书文件
			byte[] certs = ByteStreams.toByteArray(in);
			wepay = WepayBuilder.newBuilder(appId, appKey, mchId).certPasswd(certPasswd).certs(certs).build();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 动态二维码支付
	 * 
	 * @param orderNumber
	 *            订单号
	 * @return 二维码链接
	 */
	public String qrPay(String orderNumber, Integer fee) {
		QrPayRequest request = new QrPayRequest();
		request.setBody("测试订单");
		request.setClientId("127.0.0.1");
		request.setTotalFee(fee);
		request.setNotifyUrl(payNotifyUrl);
		request.setOutTradeNo(orderNumber);
		request.setTimeStart(Dates.now("yyyyMMddHHmmss"));
		return wepay.pay().qrPay(request);
	}

	public JsPayResponse jsPay(String orderNumber, String openId) {
		JsPayRequest request = new JsPayRequest();
		request.setOpenId(openId);
		request.setTotalFee(1);
		request.setOutTradeNo(orderNumber);
		request.setBody("测试订单");
		request.setClientId("127.0.0.1");
		request.setNotifyUrl(payNotifyUrl);
		request.setTimeStart(Dates.now("yyyyMMddHHmmss"));
		return wepay.pay().jsPay(request);
	}

	/**
	 * 校验签名
	 * 
	 * @param params
	 *            参数(包含sign)
	 * @return 校验成功返回true，反之false
	 */
	public Boolean verifySign(Map<String, ?> params) {
		return wepay.notifies().verifySign(params);
	}

	/**
	 * 通知成功
	 */
	public String notifyOk() {
		return wepay.notifies().ok();
	}

	/**
	 * 通知不成功
	 * 
	 * @param errMsg
	 *            错误消息
	 */
	public String notifyNotOk(String errMsg) {
		return wepay.notifies().notOk(errMsg);
	}

	public RefundApplyResponse refundApply(String orderNumber) {
		RefundApplyRequest req = new RefundApplyRequest();
		req.setOutTradeNo(orderNumber);
		req.setOutRefundNo(orderNumber);
		req.setTotalFee(1);
		req.setRefundFee(1);
		req.setOpUserId(wepay.getMchId());
		return wepay.refund().apply(req);
	}
}
